﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System;

public class CreateLuaFileTemplate : MonoBehaviour
{
    [UnityEditor.MenuItem("XLua/新建脚本")]
    [UnityEditor.MenuItem("Assets/Create/Lua脚本")]
    public static void CreateLuaFileFromTemplate()
    {
        string template = @"
--自动创建
--日期 {0}
--lua 脚本
function Awake()
    -- 初始化
end

function Start()
	-- 开始
end

function Update()
    -- 每帧更新
end

function OnDestroy()
    -- 销毁
end
";
        if (!Directory.Exists(XluaEngine.LuaSrcPath))
            Directory.CreateDirectory(XluaEngine.LuaSrcPath);
        //.AssetModificationProcessor
        string path = UnityEditor.EditorUtility.SaveFilePanelInProject("新建",
            "new luaBehavior", "lua", "取个名字，直接保存", XluaEngine.LuaSrcPath);
        if (path.Length != 0)
        {
            if (File.Exists(path))
            {
                Debug.LogError("警告 ，创建了的脚本要改名吗");
            }
            else
            {
                File.WriteAllText(path, string.Format(template, DateTime.Now.ToShortDateString()));
                AssetDatabase.Refresh();
            }
        }
    }
}
